// pages/home/service.js
const api = require("../../api/api");

Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    beforePlanned: false,
    imgPath: '/images',
    //订购总数，现在没用到
    dgTotle: 0,
    //订购数量
    dgVal: 0,
    //剩余天数
    remainingDays: '0',
    // 加入前计算剩余天数
    beforeRemainingDays: '9',
    //尊享服务列表数据
    listData: [{
      image: 'http://qnstatic.lianhequnmei.com/images/20221223/server_jo_07.png',
      title: '育婴师、营养师、早教师咨询服务',
      label: '在线时间：09:00-23:00'
    }, {
      image: 'http://qnstatic.lianhequnmei.com/images/20221223/server_jo_11.png',
      title: '线上早教课',
      label: '让家长和宝宝足不出户也能学早期教育'
    }, {
      image: 'http://qnstatic.lianhequnmei.com/images/20221227/20221227181408.png',
      title: '家庭育儿指南',
      label: '育儿知识大全百科让你带娃不再慌张'
    }],
    //个人中心
    home: null,

    //陪伴天数
    card_diff_days: 0,

    //弹窗
    flag: false,

    dailing: 3,

    // 填写宝宝信息弹窗
    member_pop: false,
    step1: true,
    step2: false

  },

  /**
   * 组件的方法列表
   */
  methods: {
    addbady_step2() {
      wx.navigateTo({
        url: '/pages/service/tianXieBaoBao',
      })
    },
    addbady_step1() {
      this.setData({
        step1: false,
        step2: true
      });
    },
    goback() {
      this.setData({
        flag: false,
      })
    },
    userCard() {
      wx.redirectTo({
        url: '/pages/chengZhangKa/index',
      })
    },
    getserver() {
      //设置tabbar激活项
      const util = require("../../utils/util")
      util.setTabbarActive('plan');
      wx.redirectTo({
        url: '/pages/plan/joinPlan',
      })
    },
    addBaby() {
      wx.navigateTo({
        url: '/pages/service/tianXieBaoBao',
      })
    },
    gooshare(){
       return;
      wx.navigateTo({
        url: '/pages/projects/projects?channel=1232323&s_share=211313&token=1006:4569df8f193dd40bbab4fe19ecafad085ba53e90e3bb3ef0dcbfd3855f28f0c8',
      })
    },
    addPlan() {

      if (this.data.home.has_baby) {

        let that = this;
        const api = require("../../api/api");
        api.user.home({
          success(res) {

            let member = res.data.data.is_member;
            let goods_id = res.data.data.member_card_info.goods_id;

        

            if (member) {
              that.setData({
                home: res.data.data,
              })

              wx.setStorageSync('home', res.data.data)

              if(goods_id == 0){
                wx.showToast({
                  title: '暂无领取',
                  icon: 'none'
                })
                 return;
              }
            
              //跳到免费订购页
              wx.navigateTo({
                url: '/pages/goods/detail?goods_id=' + goods_id,
              })
              return;
            
            } else {
              that.setData({
                flag: true,
              })
            }

            //  console.log("home,res", res.data.data.is_member);
            return;


          }
        })
      } else {

        if (this.data.home.is_member) {
          wx.showToast({
            title: '请先填写宝宝信息',
            icon: 'none'
          })
        } else {
          const util = require("../../utils/util")
          util.setTabbarActive("plan");
          wx.navigateTo({
            url: '/pages/plan/joinPlan',
            //url: '/pages/projects/projects',   //测试用
          })
        }


      }

    },
    /**
     * 点击尊享服务
     */
    customerService(e) {
      // console.log("customerService,e", e);
      let title = e.currentTarget.dataset.title;

      if (title.indexOf("咨询服务") >= 0) {
        // 咨询服务
        wx.navigateTo({
          url: '/im/pages/index?type=consultants',
        })

        wx.setStorageSync('im:redirectUrl', "/pages/consultants/consultants");

        // wx.navigateTo({
        //   url: '/pages/consultants/consultants',
        // })


      } else if (title.indexOf("线上早教课") >= 0) {
        //线上早教课
        // if (this.data.beforePlanned) {
        //   wx.showToast({
        //     title: '请先加入计划',
        //   })
        //   return;
        // }
        if (this.data.home.is_member) {
          // wx.showToast({
          //   title: '正在更新，敬请期待',
          //   icon: 'none'
          // })
          // return;
          wx.navigateTo({
            url: '/pages/earlyEducation/index',
          })
        }else{
          wx.navigateTo({
            url: '/pages/earlyEducation/index',
          })
        }
   
      } else {
        //生日大礼包
        // if (this.data.beforePlanned) {
        //   wx.showToast({
        //     title: '请先加入计划',
        //   })
        //   return;
        // }

        // wx.navigateTo({
        //   url: '/pages/person/birthdayGift',
        // })

        wx.navigateTo({
          url: '/pages/parenting/parenting',
        })

      }

    },

    /**
     * 立即领取
     */
    pickUp() {

      const that = this;
      let goods_id = null;
      if (this.data.home && this.data.home.member_card_info && this.data.home.member_card_info.goods_id) {
        goods_id = this.data.home.member_card_info.goods_id;
      }
      if (!goods_id) {
        //没有免费商品信息，查库
        api.home.home({
          success(res) {
            // console.log("home,res", res)
            that.setData({
              home: res.data.data,
            })
            goods_id = that.data.home.member_card_info.goods_id;

            //跳到免费订购页
            wx.navigateTo({
              url: '/pages/goods/detail?goods_id=' + goods_id,
            })
            return;
          }
        })
      }
      //跳到免费订购页
      wx.navigateTo({
        url: '/pages/goods/detail?goods_id=' + goods_id,
      })
    },
  },
  lifetimes: {
    attached() {
      // console.log("attached")
      const that = this;

      const util = require("../../utils/util")

      const app = getApp();
      this.setData({
        beforePlanned: app.globalData.beforePlanned,
        beforePlannedSatus: app.globalData.beforePlannedSatus,
        imgPath: app.globalData.imgPath
      });
      // console.log("service,this.data", this.data)

      //页面标题
      wx.setNavigationBarTitle({
        title: "尊享服务"
      })

      let home = wx.getStorageSync('home')
      if (!home) {
        //读取“个人中心”
        api.user.home({
          success(res) {
            // console.log("home,res", res);
            that.setData({
              home: res.data.data,
            })

            //保存home到本地缓存
            wx.setStorageSync('home', res.data.data);
          }
        })
      } else {
        // that.setData({
        //   home: home,
        // })
        api.user.home({
          success(res) {
            // console.log("home,res", res);
            that.setData({
              dailing: res.data.data.is_member ? res.data.data.member_card_info.can_get_number : 3,
              home:res.data.data
            })
          }
        })



      }

      if (home.is_member && home.has_baby == 0) {
        this.setData({
          member_pop: true
        });
      }

      //领取剩余天数
      let dtNow = new Date();
      let year1 = dtNow.getFullYear();
      let month1 = dtNow.getMonth();
      let date1 = dtNow.getDate();
      //明天
      let tomorrow = new Date(year1, month1, date1 + 1)
      //下月1日
      let dtNextMonth = new Date(year1, month1 + 1, 1);
      //相差毫秒数
      let bDt = Number(dtNextMonth) - Number(tomorrow);
      //转为天数
      let bDate = Math.round(bDt / (24 * 60 * 60 * 1000));
      //设到显示
      //  let sdata = home.member_card_info.next_goods_remain_days;
      let sdata = '';
      let pickup_total = home.is_member ? home.member_card_info.pickup_total : 3;
      let expire_total = home.member_card_info.expire_total;

      // 计算本月剩余待领取天数
      let today = new Date();
      let now = today.getDate();
      let year = today.getYear();
      if (year < 2000) year += 1900;
      let month = today.getMonth();
      let monarr = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
      if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) monarr[1] = "29";
      let ydata = monarr[month] - now;


      this.setData({
        dgVal: pickup_total - expire_total,
        remainingDays: sdata,
        beforeRemainingDays: ydata + 1,
      //  dailing: pickup_total
      })

      //陪伴成长天数
      // console.log("this.data.home", this.data.home)
      let card_start_time = new Date(home.member_card_info.start_time);
      let card_diff = Number(dtNow) - Number(card_start_time);
      let card_diff_days = Math.round(Number(card_diff) / (24 * 60 * 60 * 1000));
      this.setData({
        card_diff_days: card_diff_days,
      })

      // console.log("components,service,this.data",this.data)
    }
  }
})